package com.baizhi.service.impl;

import com.baizhi.entity.CmfzMenu;
import com.baizhi.mapper.CmfzMenuMapper;
import com.baizhi.service.CmfzMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * InnoDB free: 11264 kB 服务实现类
 * </p>
 *
 * @author xiaoxiao
 * @since 2020-08-18
 */
@Service
public class CmfzMenuServiceImpl extends ServiceImpl<CmfzMenuMapper, CmfzMenu> implements CmfzMenuService {

    @Override
    public List<CmfzMenu> menu() {
        List<CmfzMenu> menuList = baseMapper.selectList(null);
        //先获取一级菜单
        List<CmfzMenu> list = menuList.stream().filter(cmfzMenu -> {
            return cmfzMenu.getMenuParentId() == null;
        }).map(cmfzMenu -> {
            //查询下一级的数据
            return cmfzMenu.setList(getMenuList(cmfzMenu, menuList));
        }).collect(Collectors.toList());//处理结果
        return list;
    }

    private List<CmfzMenu> getMenuList(CmfzMenu cmfzMenu, List<CmfzMenu> menuList) {
        List<CmfzMenu> list = menuList.stream().filter(item -> {
            return item.getMenuParentId() == cmfzMenu.getMenuId();
        })
                .map(item -> {
                    //递归调用
                    return item.setList(getMenuList(item,menuList));
                })
                .collect(Collectors.toList());
        return list;
    }
}
